import { reqQyyjtUserToken } from '@/api/qyyjt'
import { reqQUserInfo } from '@/api/user'
import { getqAT, getqEU, getqRT, setqAT, setqEU, setqRT } from '@/utils/auth'
import { defineStore } from 'pinia'

let useQStore = defineStore('QYYJT', {
  state: () => {
    return {
      EncUser: getqEU(),
      R_TK: getqRT(),
      A_TK: getqAT(),
      qUserPhone: '',
      qUserNickname: '',
    }
  },
  actions: {
    async getQUerInfo(uuid: string) {
      let resp = await reqQyyjtUserToken(uuid)
      if (resp.data.data.status === 113) {
        setqAT(resp.data.data.token.accessToken)
        setqRT(resp.data.data.token.refreshToken)
        setqEU(resp.data.data.basic_info.encryptUser)
        this.qUserPhone = resp.data.data.basic_info.phone
        this.qUserNickname = resp.data.data.basic_info.nickname
        // 因为直接设置为localStorage之后，无法直接从store中调取数据
        this.EncUser = getqEU()
        this.R_TK = getqRT()
        this.A_TK = getqAT()
        // 放到数据库存储，下次在user信息中获取
        let qUserObj = {
          EncUser: this.EncUser,
          R_TK: this.R_TK,
          A_TK: this.A_TK,
          qUserPhone: this.qUserPhone,
          qUserNickname: this.qUserNickname,
        }
        await reqQUserInfo(qUserObj)
      }
      return resp.data.data
    },
    // updateState(){

    //   this.EncUser = getqEU()
    //   this.R_TK = getqRT()
    //   this.A_TK = getqAT()
    // }
  },
  getters: {},
})

export default useQStore
